//
//  TLLoanVC.m
//  TLLoanSimulat
//
//  Created by mc on 2017/10/23.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "TLLoanVC.h"
#import "TLLoanCell.h"

#define kTabCellID @"LoanCell"

@interface TLLoanVC ()<UITableViewDataSource,UITableViewDelegate> {
    NSArray *_dataArray;
    NSArray *_indexArray;
}
@property (unsafe_unretained, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation TLLoanVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self initData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 初始化
#pragma mark -
#pragma mark ---初始化UI
-(void)initUI {
    [self initTable];
}
#pragma mark ---初始化数据
-(void)initData {
    
    _indexArray = @[@"A",@"B",@"D"];
    _dataArray = @[@{@"key":@"A",@"array":@[@"阿珂",@"阿牛",@"啊",@"arena",@"asuro"]},
                   @{@"key":@"B",@"array":@[@"比人阿珂",@"比方阿牛",@"把啊",@"包子",@"把妹教学",@"把柄"]},
                   @{@"key":@"D",@"array":@[@"低人阿珂",@"地上阿牛",@"地板",@"地三鲜",@"帝王将相",@"递归"]}];
    [_tableView reloadData];
}


#pragma mark - 列表
#pragma mark -
#pragma mark ---初始化 tab
- (void)initTable
{
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //分割线
//    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //更改索引的背景颜色:
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //更改索引的文字颜色:
    self.tableView.sectionIndexColor = UIColorFormRGB_theme;
    [self registerMyBillCell];
}


#pragma mark - 列表相关
#pragma mark -
#pragma mark ---注册CELL
- (void)registerMyBillCell
{
    ///活动列表 cell
    [_tableView registerNib:[UINib nibWithNibName:(NSStringFromClass([TLLoanCell class])) bundle:nil] forCellReuseIdentifier:kTabCellID];
    [self setExtraCellLineHidden:_tableView];
}

#pragma mark ---隐藏多余的线
-(void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

//返回每个Section的title标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[_dataArray objectAtIndex:section]objectForKey:@"key"];
}

//返回索引栏数据
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return _indexArray;
}
//建立索引栏和section的关联
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return index;
}

#pragma mark ---行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark ---组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  _dataArray.count;
}

#pragma mark ---行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *list=[[_dataArray objectAtIndex:section]objectForKey:@"array"];
    return (list.count-1)/2+1;
}

#pragma mark ---组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

#pragma mark ---组脚高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000000001;
}

#pragma mark ---行设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TLLoanCell*cell= [tableView dequeueReusableCellWithIdentifier:kTabCellID];
    if (!cell) {
        cell = [[TLLoanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTabCellID];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    NSDictionary *dict = _dataArray[indexPath.section];
    NSArray *array =  dict[@"array"];
    
    NSInteger row = indexPath.row*2;
    if (row+1 > array.count-1) {
        [cell setCellWithModel:array[row] NextModel:nil];
        return cell;
    }
    [cell setCellWithModel:array[row] NextModel:array[row+1]];
    return cell;
}
@end
